package com.ly.honor.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ly.honor.entity.GoodsOrder;
import com.ly.honor.entity.dto.GoodsQueryDTO;
import com.ly.honor.entity.dto.OrderCreateDTO;
import com.ly.honor.entity.dto.OrderEditDTO;
import com.ly.honor.entity.dto.OrderQueryDTO;
import com.ly.honor.entity.vo.GoodsQueryVO;
import com.ly.honor.entity.vo.OrderQueryVO;
import com.ly.honor.service.GoodsOrderService;
import com.ly.honor.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * (GoodsOrder)表控制层
 *
 * @author makejava
 * @since 2023-03-31 17:20:12
 */
@RestController
@RequestMapping("order")
@Validated
@Api(tags = "订单管理")
public class GoodsOrderController {
    /**
     * 服务对象
     */
    @Resource
    private GoodsOrderService goodsOrderService;

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @PostMapping("getOrderPage/{pageNum}/{pageSize}")
    @ApiOperation("分页查询")
    public Result getOrderPage(@PathVariable("pageNum") long pageNum,
                               @PathVariable("pageSize") long pageSize,
                               @RequestBody(required = false) OrderQueryDTO orderQueryDTO) {
        Page<OrderQueryVO> orderQueryVOPage = goodsOrderService.getOrderPage(pageNum,pageSize,orderQueryDTO);
        return Result.ok(orderQueryVOPage);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("getOrderById/{id}")
    @ApiOperation("通过主键查询单条数据")
    public Result getOrderById(@PathVariable("id") Integer id) {
        OrderQueryVO orderQueryVO = goodsOrderService.getOrderById(id);
        return Result.ok(orderQueryVO);
    }


    @GetMapping("getOrderByOrderStatus/{orderStatus}")
    @ApiOperation("通过订单状态查询数据")
    public Result getOrderByOrderStatus(@PathVariable("orderStatus") Integer orderStatusId,HttpServletRequest request) {
        List<OrderQueryVO> orderQueryVOList = goodsOrderService.getOrderByOrderStatus(orderStatusId,request);
        return Result.ok(orderQueryVOList);
    }

    @GetMapping("getOrderByUser")
    @ApiOperation("根据用户查看所有订单")
    public Result getOrderByUser(HttpServletRequest request) {
        List<OrderQueryVO> orderQueryVOList = goodsOrderService.getOrderByUser(request);
        return Result.ok(orderQueryVOList);
    }

    /**
     * 新增数据
     *
     * @param orderCreateDTO 实体
     * @return 新增结果
     */
    @PostMapping("addOrder")
    @ApiOperation("新增数据")
    public Result addOrder(@RequestBody @Valid OrderCreateDTO orderCreateDTO, HttpServletRequest request) {
        var orderNumber = goodsOrderService.addOrder(orderCreateDTO,request);
        return Result.ok(orderNumber);
    }

    /**
     * 编辑数据
     *
     * @param orderEditDTO 实体
     * @return 编辑结果
     */
    @PostMapping("updateOrder")
    @ApiOperation("编辑数据")
    public Result updateOrder(@RequestBody @Valid OrderEditDTO orderEditDTO) {
        goodsOrderService.updateOrder(orderEditDTO);
        return Result.ok();
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("deleteOrderById/{id}")
    public Result deleteOrderById(@PathVariable Integer id) {
        goodsOrderService.removeById(id);
        return Result.ok();
    }

    @DeleteMapping("removeBatch")
    @ApiOperation("批量删除订单")
    public Result removeBatch(@RequestBody List<Long> idList) {
        goodsOrderService.removeByIds(idList);
        return Result.ok();
    }

    @PostMapping("updateOrderStatus")
    @ApiOperation("通过id修改商品状态")
    public Result updateOrderStatus(Integer id,
                                    Integer orderStatusId) {
        goodsOrderService.updateOrderStatus(id,orderStatusId);
        System.out.println(id);
        System.out.println(orderStatusId);
        System.out.println("签收");
        return Result.ok();
    }

}

